﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExitGames.Client.Photon;
using Google.Protobuf;
using PhotonServerCommon.Enums;
using PhotonServerCommon.Messages;
using PhotonServerCommon.Messages.Helper;
using PhotonTestClient.Helper;

namespace PhotonTestClient.Handler
{
    public class RealmHandler
    {
        public PhotonTestClient OwningTestClient { get; set; }

        public void HandleRealmMessage(OperationResponse operationResponse)
        {
            byte telegramCode = operationResponse.OperationCode;
            int telegramSubCode = (int) operationResponse.Parameters[(byte) ParameterCodes.OperationSubCode];

            if (telegramCode == (byte) OperationCodes.Transfer)
            {
                switch (telegramSubCode)
                {
                    case (int) OperationSubCodes.RequestRealmList:
                    {
                        RealmList realmList =
                            MessageSerializer.DeserializeMessage<RealmList>((byte[])
                                operationResponse.Parameters[(byte) ParameterCodes.RealmList]);

                        foreach (RealmServer realmServer in realmList.RealmServer)
                        {
                            string realmEntry =
                                $"Name = {realmServer.RealmName} - ServerId = {realmServer.RealmServerId} - CurPop = {realmServer.CurrentPopulation} - MaxPop = {realmServer.MaximumPopulation} - Queue = {realmServer.Queue}";

                                OwningTestClient.SetStatusText(LogHelper.GetTimeStamp(), realmEntry);
                        }

                        break;
                    }
                }
            }

        }
    }
}
